const App = getApp();
const wxParse = require('../../wxParse/wxParse.js')
const qingxilist = require('../../utils/qingxilist.js')
const siteinfo = require('../../siteinfo.js')

Page({

  data: {
    // 页面参数
    qingxilist: qingxilist,
    options: {},
    // 页面元素
    scrollTop: 0,
    index: 0,
    detail: {},
    IsGongshui: '',
    ZiXi:'',
    tuResult: true, //检测一下是不是有没有图片未传完
    XianChangZhaoPian: [],
    GongZuoZhaoPian: [],
    WenTiZhaoPian: [],
    qingkong: '',
    ErGongShuiChiShuiZhiJianCeShiJian: '',
    TianBiaoRiQi: '',
    ErGongSheShiQingXiShiJian: '',
    GuanWangShiYongShiJian: '',
    ErGongSheShiJianCeShiJian: '',
    ErGongJiaYaSheBeiTiaoYaShiJian: '',
    YouZhiYongShuiShiJian: '',
    ErGongSheShiQingXiJiGou: '',
    ErGongXingShi: '', //二供形式，用于检测是否无负压供水
    LvHuaYongShui: '', //绿化水，用户检测是否中水
    // XiaoQuleiXing:'',//小区类型
    $data: {},
    qingkongList: [], //搜索的机构的list
    XiaoQuleiXingList: [{
        value: '居民集资房'
      },
      {
        value: '居民自建房'
      },
      {
        value: '统建楼'
      },
      {
        value: '其它'
      }
    ],
    weiyiyuanList: [{
        value: '未纳入改造'
      },
      {
        value: '物业管理单位无改造意愿'
      },
    ],
    isYouwuList: [{
        value: '有'
      },
      {
        value: '无'
      },
    ],
    isShifouList: [{
        value: '是'
      },
      {
        value: '否'
      },
    ],
    ErGongXingShiList: [{
        value: '管道泵直抽供水'
      },
      {
        value: '生活水池+水泵'
      },
      {
        value: '生活水池+水泵+天面池'
      },
      {
        value: '水泵+天面池'
      },
      {
        value: '其它'
      },
    ],
    ErGongXiaoFangJieHeList: [{
        value: '生活水池与消防水池共用'
      },
      {
        value: '生活水池与消防水池独立'
      }, {
        value: '弃用生活用途，仅为消防用途'
      },
      {
        value: '其它'
      },
    ],
    ErGongDiXiaShuiChiCaiZhiList: [{
        value: '瓷砖内饰面'
      },
      {
        value: '不锈钢'
      },
      {
        value: '其它'
      },
    ],
    ErGongShuiChiList: [{
        value: '渗漏'
      },
      {
        value: '溢流'
      },
      {
        value: '池壁卫生不良'
      },
    ],
    ErGongJiaYaSheBeiList: [{
        value: '管道泵'
      },
      {
        value: '恒压（工频）泵'
      },
      {
        value: '变频泵'
      },
      {
        value: '其它'
      }
    ],
    ShuiBengGuoLiuCaiZhiList: [{
        value: '不锈钢'
      },
      {
        value: '铸铁'
      },
      {
        value: '其它'
      },
    ],
    YuanChengGuanLiList: [{
        value: '实现远程智能监控管理'
      },
      {
        value: '未实现远程智能监控管理'
      },
    ],
    RenYuanPeiBeiList: [{
        value: '专人'
      },
      {
        value: '兼职'
      },
    ],
    LvHuaYongShuiList: [{
        value: '自来水'
      },
      {
        value: '中水'
      },
      {
        value: '其它'
      }
    ],
    GuanGaiMoShiList: [{
        value: '滴灌'
      },
      {
        value: '喷灌'
      },
      {
        value: '漫灌'
      },
      {
        value: '其它'
      }
    ],
    jigouList: [{
        value: '业主自管'
      },
      {
        value: '其它'
      }
    ],
    danweiList: [{
        value: '物业管理单位'
      },
      {
        value: '其它'
      }
    ],
    YiLiuGuanJuTiList: [{
        value: '空气隔断'
      },
      {
        value: '倒流防止器          '
      }
    ],
    FenQuXingShiList: [{
        value: '高、低两区'
      },
      {
        value: '高、中、低三区'
      }
    ],
    JinChuShuiGuanCaiZhiList: [{
        value: '镀锌钢管'
      },
      {
        value: '不锈钢管'
      }, {
        value: '钢塑复合管'
      },
      {
        value: '其它'
      }
    ],
    ZhiNengGuanLiList: [{
        value: '实现远程智能自动化管理'
      },
      {
        value: '未实现远程智能自动化管理'
      }
    ],
    addr: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let arr = {
      ..._this.data
    }
    delete arr.$data
    this.setData({
      $data: arr
    })
  },

  getDetail(e) {
    let _this = this
    App._get("GetShiYaoProjectInfo", {
      id: e,
      userId: wx.getStorageSync('user_id') ? wx.getStorageSync('user_id') : ''
    }, (res) => {
      console.log(res)
      _this.setData({
        detail: res
      })
      wxParse.wxParse('article', 'html', res.YaoQiu, _this, 0)
    })
  },

  radioChange(e) {},
  radioChangeErGongSheShiGuanLiJiGou(e){
    this.setData({
      ErGongSheShiGuanLiJiGou: e.detail.value
    })
  },
  radioChangeZiXi(e){
    this.setData({
      ZiXi: e.detail.value
    })
  },
  radioChangeErGongXingShi(e) {
    this.setData({
      ErGongXingShi: e.detail.value
    })
  },
  radioChangeIsGongshui(e) {
    this.setData({
      IsGongshui: e.detail.value
    })
  },
  radioChangeLvHuaYongShui(e) {
    this.setData({
      LvHuaYongShui: e.detail.value
    })
  },
  radioChangeXiaoQuleiXing(e) {
    this.setData({
      ChengZhongCunleiXing: e.detail.value
    })
  },
  radioChangeErGongXiaoFangJieHe(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongXiaoFangJieHe: e.detail.value
    })
  },
  radioChangeErGongDiXiaShuiChiCaiZhi(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongDiXiaShuiChiCaiZhi: e.detail.value
    })
  },
  radioChangeErGongJiaYaSheBei(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongJiaYaSheBei: e.detail.value
    })
  },
  radioChangeShuiBengGuoLiuCaiZhi(e) {
    console.log(e.detail.value)
    this.setData({
      ShuiBengGuoLiuCaiZhi: e.detail.value
    })
  },

  selectAddress() {
    App.getPermission(this)
  },
  formSubmit(e) {
    var _this = this
    let data = {
      ...e.detail.value
    }
    try {
      if (_this.data.loading) {
        return
      }
      _this.setData({
        loading: true
      })
      data.TianBiaoRiQi = this.data.TianBiaoRiQi
      data.ErGongSheShiQingXiShiJian = this.data.ErGongSheShiQingXiShiJian
      data.ErGongSheShiJianCeShiJian = this.data.ErGongSheShiJianCeShiJian
      data.ErGongJiaYaSheBeiTiaoYaShiJian = this.data.ErGongJiaYaSheBeiTiaoYaShiJian
      data.XianChangZhaoPian = this.data.XianChangZhaoPian.join(",")
      data.GongZuoZhaoPian = this.data.GongZuoZhaoPian.join(",")
      data.WenTiZhaoPian = this.data.WenTiZhaoPian.join(",")
    } catch (e) {
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel: false,
        title: '错误',
        content: 'e'
      })
    }
    wx.showLoading({
      title: '提交中',
    })
    // return
    App._post_form(e.detail.target.dataset.xingwei == "tijiao" ? 'ChengZhongCunShenHe' : 'SaveChengZhongCunXunCha', data, (res) => {
      this.setData(_this.data.$data)
      wx.showModal({
        title: res,
        showCancel: false,
        complete: () => {
          // wx.navigateBack()
          _this.setData({
            loading: false
          })
        }
      })
    }, () => {}, () => {
      wx.hideLoading()
    })
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.detail.Name,
      path: `/pages/xiaoqu/idnex`,
    };
  },
  bindDateChange(e) { //填表时间
    this.setData({
      TianBiaoRiQi: e.detail.value
    })
  },

  bindDateChangeErGongSheShiQingXiShiJian(e) { //清洗时间
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ErGongSheShiQingXiShiJian: e.detail.value
    })
  },
  bindDateErGongSheShiJianCeShiJian(e) { // 检测时间
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ErGongSheShiJianCeShiJian: e.detail.value
    })
  },

  bindErGongSheShiQingXiJiGou(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      bindErGongSheShiQingXiJiGou: e.detail.value,
      index: e.detail.value
    })
  },

  radioChangeJinChuShuiGuanCaiZhi(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      JinChuShuiGuanCaiZhi: e.detail.value
    })
  },
  radioChangeErGongTianMianShuiChiCaiZhi(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ErGongTianMianShuiChiCaiZhi: e.detail.value
    })
  },
  radioChangeYongZhiYongShuiYiLiXiang(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      YongZhiYongShuiYiLiXiang: e.detail.value
    })
  },
  radioChangeGuanGaiMoShi(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      GuanGaiMoShi: e.detail.value
    })
  },
  radioChangeJieShuiQiJuGuanXiaDanWei(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      JieShuiQiJuGuanXiaDanWei: e.detail.value
    })
  },
  blurLianXiFangShi(e) {
    // if (e.detail.value.length != "11") {
    //   wx.showModal({
    //     showCancel: false,
    //     title: '手机号必须为11位'
    //   })
    // }
  },
  shanchu(e) {
    //  e.target.name
    let _this = this
    console.log(e)
    let arr = []
    arr = arr.concat(_this.data[e.target.dataset.name])
    arr.splice(e.target.dataset.index, 1)
    this.setData(_this.setKeyAndValue(e.target.dataset.name, arr))
  },
  setKeyAndValue(key, value) {
    var obj = {};
    console.log(key)
    obj[key] = value
    console.log(obj)
    return obj
  },
  searchQingkong(e) {
    let _this = this;
    App._get('GetQingXiJiGou', {
      name: e.detail.value
    }, (res) => {
      console.log(res)
      if (res.length === 0) {
        res = [{
          Name: '暂无数据'
        }]
      }
      _this.setData({
        qingkongList: res
      })
    })
  },
  selectQingkong(e) {
    this.setData({
      qingkongList: [],
      ErGongSheShiQingXiJiGou: e.currentTarget.dataset.name
    })
  },
  bindXianChangZhaoPian() {
    let _this = this
    var XianChangZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult: false
        })
        for (let i in res.tempFiles) {
          let promise = new Promise((rel, rej) => {
            App.uploaFile('/api/UpLoadProcess', res.tempFiles[i].tempFilePath, (res2) => {
              rel(siteinfo.siteroot + res2)
            })
          })
          arr.push(promise)
        }
        Promise.all(arr).then((result) => {
          console.log(result, '图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.XianChangZhaoPian)
          arrList.push.apply(arrList, result)
          _this.setData({
            XianChangZhaoPian: arrList,
            tuResult: true
          })
        })
      }
    })
  },
  bindGongZuoZhaoPian() {
    let _this = this
    var GongZuoZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult: false
        })
        for (let i in res.tempFiles) {
          let promise = new Promise((rel, rej) => {
            App.uploaFile('/api/UpLoadProcess', res.tempFiles[i].tempFilePath, (res2) => {
              rel(siteinfo.siteroot + res2)
            })
          })
          arr.push(promise)
        }
        Promise.all(arr).then((result) => {
          console.log(result, '图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.GongZuoZhaoPian)
          arrList.push.apply(arrList, result)
          _this.setData({
            GongZuoZhaoPian: arrList,
            tuResult: true
          })
        })
      }
    })
  },
  bindWenTiZhaoPian() {
    let _this = this
    var WenTiZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult: false
        })
        for (let i in res.tempFiles) {
          let promise = new Promise((rel, rej) => {
            App.uploaFile('/api/UpLoadProcess', res.tempFiles[i].tempFilePath, (res2) => {
              rel(siteinfo.siteroot + res2)
            })
          })
          arr.push(promise)
        }
        Promise.all(arr).then((result) => {
          console.log(result, '图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.WenTiZhaoPian)
          arrList.push.apply(arrList, result)
          _this.setData({
            WenTiZhaoPian: arrList,
            tuResult: true
          })
        })
      }
    })
  },
  shanchu(e) {
    //  e.target.name
    let _this = this
    console.log(e)
    let arr = []
    arr = arr.concat(_this.data[e.target.dataset.name])
    arr.splice(e.target.dataset.index, 1)
    this.setData(_this.setKeyAndValue(e.target.dataset.name, arr))
  },
  setKeyAndValue(key, value) {
    var obj = {};
    console.log(key)
    obj[key] = value
    console.log(obj)
    return obj
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 获取首页数据
    wx.stopPullDownRefresh();
  },
  /**
   * 滚动到底部
   */
  goBottom() {
    var obj = wx.createSelectorQuery();
    obj.selectAll('.container').boundingClientRect(function (rect) {
      wx.pageScrollTo({
        scrollTop: rect[0].height,
        duration: 300
      });
    })
    obj.exec();
  },
  onShareAppMessage() {
    return {
      title: '城中村巡查',
      path: "/pages/chengzhongcun/index"
    };
  },

});